package com.prd.db.biz;

import com.prd.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author lizhaoyi
 * @date 2023/11/6 22:01
 * Description:
 */

@SpringBootTest
@Slf4j
@RunWith(SpringRunner.class)
public class test {

    @Test
    public void authenticate(){
        String host = "https://zid.market.alicloudapi.com";
        String path = "/idcheck/Post";
        String method = "POST";
        String appcode = "prd";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("cardNo", "370323198308203017");
        bodys.put("realName", "任洪宾");

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            int httpCode= response.getStatusLine().getStatusCode();
            if (httpCode == 200) {

                System.out.println("正常请求计费(其他均不计费)");
                System.out.println("获取返回的json：");
                System.out.println(EntityUtils.toString(response.getEntity()));
            } else {

                // Map<String, List<String>> map = response.getAllHeaders().map();
                //String error = map.get("X-Ca-Error-Message").get(0);
                String error = response.getFirstHeader("X-Ca-Error-Message").getValue();
                if (httpCode == 400 && error.equals("Invalid AppCode `not exists`")) {
                    System.out.println("AppCode错误 ");
                } else if (httpCode == 400 && error.equals("Invalid Url")) {
                    System.out.println("请求的 Method、Path 或者环境错误");
                } else if (httpCode == 400 && error.equals("Invalid Param Location")) {
                    System.out.println("参数错误");
                } else if (httpCode == 403 && error.equals("Unauthorized")) {
                    System.out.println("服务未被授权（或URL和Path不正确）");
                } else if (httpCode == 403 && error.equals("Quota Exhausted")) {
                    System.out.println("套餐包次数用完 ");
                } else {
                    System.out.println("参数名错误 或 其他错误");
                    System.out.println(error);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void sendMessage(){

        //短信api调用地址
        String host = "https://dfsns.market.alicloudapi.com";
        //请求路径
        String path = "/data/send_sms";
        //请求方式
        String method = "POST";
        //appcode：自己的AppCode，可以在自己的控制台找到，是一串32位的字符串
        String appcode = "14xfa0523x3s2fe8ep0u54te14c2w36ad";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();

        //使用随机数生成4位验证码
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int randomNum = (int) (Math.random()*10);
            stringBuilder.append(randomNum);
        }
        String code = stringBuilder.toString();
        //这里是要发送给用户的验证码，我们使用了随机数进行生成
        bodys.put("content", "code:"+code);
        //这里是接受短信的手机号码，我们在实际开发时这里就不能写死了
        bodys.put("phone_number", "176****5321");
        //这里是发送短信的模板ID：代表整体短信文案模板
        bodys.put("template_id", "TPL_0000");
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
